<?php
class VideoApp extends BackendApp
{
	var $_video_mod;

	function __construct()
	{
		$this->VideoApp();
	}

	function VideoApp()
	{
		parent::BackendApp();

		$this->_video_mod =& m('video');
	}

	function drop()
	{
		$db=&db();
		$id = isset($_GET['id']) ? trim($_GET['id']) : '';
		if (!$id)
		{
			$this->show_warning('没有选择任何数据');
			return;
		}

		$ids = explode(',', $id);

		if (!$this->_video_mod->drop($ids))
		{
			$this->show_warning($this->_video_mod->get_error());

			return;
		}

		foreach ($ids as $k=>$v){
			$sql="select file_path from cw_video where video_id=".$v;
			$file_path=$db->getOne($sql);
				
			@unlink(ROOT_PATH.'/'.$file_path);
		}

		$this->show_message('删除成功');
	}

	function index()
	{
		$checked = $_GET['checked'];
		$conditions = '';
		if($checked==='0' || $checked==='1'){
			$conditions.='AND checked = '.$checked;
		}
		$conditions .= $this->_get_query_conditions(array(
		array(
                'field' => 'content',         //可搜索字段title
                'equal' => 'LIKE',          //等价关系,可以是LIKE, =, <, >, <>
                'assoc' => 'AND',           //关系类型,可以是AND, OR
                'name'  => 'content',         //GET的值的访问键名
                'type'  => 'string',        //GET的值的类型
		),
		));
		$page   =   $this->_get_page(10);   //获取分页信息
		$sql_count = "SELECT COUNT(*) FROM cw_video AS cv
					LEFT JOIN cw_member AS cm ON cv.uid = cm.user_id 
					WHERE 1=1 ".$conditions." ORDER BY cv.sort_order ASC";
		$res_count = $this->_video_mod->getOne($sql_count);

		$sql = "SELECT cv.*,cm.user_name,cm.real_name FROM cw_video AS cv
				LEFT JOIN cw_member AS cm ON cv.uid = cm.user_id 
				WHERE 1=1 ".$conditions." ORDER BY cv.sort_order ASC LIMIT ".$page['limit'];
		$videos = $this->_video_mod->getAll($sql);
		$page['item_count']=$res_count;   //获取统计数据
		$this->_format_page($page);
		$this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条

		$checked = array(
		0 => '未审核',
		1 => '已审核',
		);

		$this->assign('checked', $checked); //分类树
		$this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
		$this->import_resource(array('script' => 'inline_edit.js'));
		$this->assign('videos',$videos);
		$this->display('video.index.html');
	}
	function add()
	{
		$vcategory_mod = & m('vcategory');
		if (!IS_POST)
		{
			$this->import_resource('jquery.plugins/jquery.validate.js');
			$sql_vcategory = 'select cate_name from cw_vcategory where 1=1';
			$vcategorys = $vcategory_mod->getAll($sql_vcategory);

			$this->assign('vcategorys',$vcategorys);
			$this->assign('video', array('sort_order' => 255));
			$this->display('video.from.html');
		}
		else
		{
			$data = array(
                'cate' => $_POST['cate'],
            	'uid' => $this->visitor->get('user_id'),
                'content' => $_POST['content'],
                'hot' => $_POST['hot'],
                'rec' => $_POST['rec'],
                'checked' => $_POST['checked'],
                'sort_order' => $_POST['sort_order'],
                'pubdate' => gmtime(),
                'checknote' => $_POST['checknote'],
			);
			//上传视频
			$names = $_FILES['file_path']['name'];
			if($names){
				$names=explode('.', $names);
				$name=$names[0];
				$types=$_FILES["file_path"]['type'];
				$types=explode('/', $types);
				$type=$types[1];
				if($type=='mp4'){
					$type='video';
				}
			}
			$image_files=$_FILES['file_path'];//上传文件

			if($image_files['name']){
				$Cfile       =   $this->_upload_video($id);
				if($type=='video'){ // mp4格式
					/*if(extension_loaded('ffmpeg')){//判断ffmpeg是否载入
						$mov = new ffmpeg_movie(ROOT_PATH.'/'.$Cfile);//视频的路径
						if($mov){
							$ff_frame = $mov->getFrame(10);
							if($ff_frame)
							{
								$gd_image = $ff_frame->toGDImage();
								$pic="/data/files/mall/videofile/movie/".time().".jpg";//要生成图片的绝对路径
								imagejpeg($gd_image, ROOT_PATH.'/'.$pic);//创建jpg图像
								imagedestroy($gd_image);//销毁一图像
							}
						}
					}*/
				}
			}
			if($Cfile){
				$data['file_path']=substr($Cfile,1);
				$data['pic']=substr($pic,1);
			}
			if($_FILES["pic"]['name']){
				$pic       =   $this->_upload_videofilefengmian($id,'movie');
				$data['pic']=substr($pic,1);
			}
			if(!$this->_video_mod->add($data)){
				$this->show_warning('操作失败！');
				return;
			}
			$this->show_message('操作成功',
                'back_list',    'index.php?app=video',
                '查看添加', 'index.php?app=video&amp;act=edit&amp;id='.$id
			);
		}
	}
	//编辑
	function edit(){
		$vcategory_mod = & m('vcategory');
		$id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if(!IS_POST){
			$sql = 'SELECT * FROM cw_video WHERE video_id ='.$id;
			$video = $this->_video_mod->getRow($sql);
			if(!$id || !$video){
				$this->show_warning('没有该视频记录！');
				return;
			}
			$sql_vcategory = 'select cate_name from cw_vcategory where 1=1';
			$vcategorys = $vcategory_mod->getAll($sql_vcategory);

			$this->assign('vcategorys',$vcategorys);
			$this->assign('ids',$id);
			$this->assign('video',$video);
			$this->display('video.from.html');
		}else{
			//$ids = isset($_POST['ids'])?intval($_POST['ids']):0;
			$data = array(
                'cate' => $_POST['cate'],
                'content' => $_POST['content'],
                'hot' => $_POST['hot'],
                'rec' => $_POST['rec'],
                'checked' => $_POST['checked'],
                'sort_order' => $_POST['sort_order'],
                'pubdate' => gmtime(),
                'checknote' => $_POST['checknote'],
			);
			//上传视频
			$names = $_FILES['file_path']['name'];
			if($names){
				$names=explode('.', $names);
				$name=$names[0];
				$types=$_FILES["file_path"]['type'];
				$types=explode('/', $types);
				$type=$types[1];
				if($type=='mp4'){
					$type='video';
				}
			}
			$image_files=$_FILES['file_path'];//上传文件

			if($image_files['name']){
				$data['uid'] = $this->visitor->get('user_id');
				$Cfile       =   $this->_upload_video($id);
				if($type=='video'){ // mp4格式
					/*if(extension_loaded('ffmpeg')){//判断ffmpeg是否载入
						$mov = new ffmpeg_movie(ROOT_PATH.'/'.$Cfile);//视频的路径
						if($mov){
							$ff_frame = $mov->getFrame(10);
							if($ff_frame)
							{
								$gd_image = $ff_frame->toGDImage();
								$pic="/data/files/mall/videofile/movie/".time().".jpg";//要生成图片的绝对路径
								imagejpeg($gd_image, ROOT_PATH.'/'.$pic);//创建jpg图像
								imagedestroy($gd_image);//销毁一图像
							}
						}
					}*/
				}
			}
			if($Cfile){
				$data['file_path']=substr($Cfile,1);
				$data['pic']=substr($pic,1);
			}
			if($_FILES["pic"]['name']){
				$pic       =   $this->_upload_videofilefengmian($id,'movie');
				$data['pic']=substr($pic,1);
			}
			if(!$this->_video_mod->edit($id,$data)){
				$this->show_warning('操作失败！');
				return;
			}
			$this->show_message('操作成功',
                'back_list',    'index.php?app=video',
                '返回详情', 'index.php?app=video&amp;act=edit&amp;id='.$id
			);
		}

	}
	//查看视频
	function video(){
		$id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if(!$id){
			$this->show_warning('没有改视频记录！');
			return;
		}
		$sql = 'SELECT file_path FROM cw_video WHERE video_id ='.$id;
		$video = $this->_video_mod->getRow($sql);
		if(!$video){
			$this->show_warning('该视频不存在！');
			return;
		}
		$this->assign('video',$video);
		$this->display('video.video.html');
	}
	function Auditing(){
		$ids = isset($_GET['id']) ? trim($_GET['id']) : '';
		if (!IS_POST){
			if (!$ids)
			{
				$this->show_warning('没有改视频记录！');
				return;
			}

			$this->assign('ids',$ids);
			$this->display('video.from.html');
		}else{
			$ids = isset($_POST['ids']) ? trim($_POST['ids']) : '';
			$checked = $_POST['checked']?$_POST['checked']:0;
			$checknote = $_POST['checknote']?$_POST['checknote']:'';
			$data = array(
				'checked'=>$checked,
				'checknote'=>$checknote,
			);

			$ids = explode(',', $ids);//获取一个类似array(1, 2, 3)的数组
			foreach ($ids as $key=>$val)
			{
				$res = $this->_video_mod->edit($val,$data);
				if(!$res){
					$this->show_warning('审核失败！');
					return;
				}
			}
			$this->show_message('提交成功！',
                'back_list',    'index.php?app=video'
                );
		}
	}
	//异步修改数据
	function ajax_col()
	{
		$id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$column = empty($_GET['column']) ? '' : trim($_GET['column']);
		$value  = isset($_GET['value']) ? trim($_GET['value']) : '';
		$data   = array();


		if (in_array($column ,array('hot', 'rec','sort_order')))
		{
			$data[$column] = $value;
			$this->_video_mod->edit($id, $data);
			if(!$this->_video_mod->has_error())
			{
				echo ecm_json_encode(true);
			}
		}
		else
		{
			return ;
		}
		return ;
	}
	//上传图片
	function _upload_logo($brand_id)
	{
		$file = $_FILES['pic'];
		if ($file['error'] == UPLOAD_ERR_NO_FILE) // 没有文件被上传
		{
			return '';
		}
		import('uploader.lib');             //导入上传类
		$uploader = new Uploader();
		$uploader->allowed_type(IMAGE_FILE_TYPE); //限制文件类型
		$uploader->addFile($_FILES['pic']);//上传logo

		if (!$uploader->file_info())
		{
			$this->show_warning($uploader->get_error() , 'go_back', 'index.php?app=video&amp;act=edit&amp;id=' . $brand_id);
			return false;
		}
		// 指定保存位置的根目录
		$uploader->root_dir(ROOT_PATH);
		$imgname=gmtime();
		// 上传
		if ($file_path = $uploader->save('data/files/mall/video', $imgname))   //保存到指定目录，并以指定文件名$brand_id存储
		{
			return $file_path;
		}
		else
		{
			return false;
		}
	}
	//上传视频
	function _upload_video($brand_id)
	{

		$file = $_FILES['file_path'];
		if ($file['error'] == UPLOAD_ERR_NO_FILE) // 没有文件被上传
		{
			return '';
		}
		import('uploader.lib');             //导入上传类
		$uploader = new Uploader();
		$uploader->allowed_type(COURSW_FILE_TYPE); //限制文件类型
		$uploader->addFile($_FILES['file_path']);//上传视频

		if (!$uploader->file_info())
		{
			$this->show_warning($uploader->get_error() , 'go_back', 'index.php?app=video&amp;act=edit&amp;id=' . $brand_id);
			return false;
		}
		// 指定保存位置的根目录
		$uploader->root_dir(ROOT_PATH);
		$imgname=md5(gmtime());
		// 上传
		if ($file_path = $uploader->save('/data/files/mall/videofile', $imgname))   //保存到指定目录，并以指定文件名$brand_id存储
		{
			return $file_path;
		}
		else
		{
			return false;
		}
	}
	function _upload_videofilefengmian($brand_id,$ptah)
	{

		$file = $_FILES['pic'];
		if ($file['error'] == UPLOAD_ERR_NO_FILE) // 没有文件被上传
		{
			return '';
		}
		import('uploader.lib');             //导入上传类
		$uploader = new Uploader();
		$uploader->allowed_type(IMAGE_FILE_TYPE); //限制文件类型
		$uploader->addFile($_FILES['pic']);//上传logo

		if (!$uploader->file_info())
		{
			//$this->show_warning($uploader->get_error() , 'go_back', 'index.php?app=course&amp;act=edit&amp;id=' . $course_id);
			//	echo "<script>parent.location.reload();</script>";
			return false;
		}
		// 指定保存位置的根目录
		$uploader->root_dir(ROOT_PATH);
		$imgname=md5(gmtime());
		// 上传

		if($ptah){
			$file_path= $uploader->save('/data/files/mall/videofile/'.$ptah, $imgname);
		}else {
			$file_path= $uploader->save('/data/files/mall/videofile/', $imgname);
		}

		if ($file_path )   //保存到指定目录，并以指定文件名$brand_id存储
		{
			return $file_path;
		}
		else
		{
			return false;
		}
	}
}